package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.context.LoginUserContext;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.*;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired(required = false)
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired(required = false)
    private GraphInfoMapper graphInfoMapper;
    @Autowired(required = false)
    private AttrValueMapper attrValueMapper;
    @Autowired(required = false)
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired(required = false)
    private LabelInfoMapper labelInfoMapper;
    @Autowired(required = false)
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired(required = false)
    private FeeValueMapper feeValueMapper;
    @Autowired(required = false)
    private LeaseTermMapper leaseTermMapper;

    @Autowired
    private BrowsingHistoryService browsingHistoryService;

    @Override
    public IPage<RoomItemVo> pageItem(Page<Object> objectPage, RoomQueryVo queryVo) {
        return roomInfoMapper.pageItem(objectPage, queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();

        RoomInfo roomInfo = this.getById(id);
        ApartmentItemVo apartmentItemVo = apartmentInfoService.selectApartmentInfoByApartmentid(roomInfo.getApartmentId());
        List<GraphInfo> graphInfoList = graphInfoMapper.selectGraphInfoListByRoomId(id);
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectAttrValueListByRoomId(id);
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectFacilityInfoListByRoomId(id);
        List<LabelInfo> labelInfoList = labelInfoMapper.selectLabelInfoListByRoomId(id);
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectPaymentTypeListByRoomId(id);
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectFeeValueVoListByApartmentId(roomInfo.getApartmentId());
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectLeaseTermListByRoomId(id);
        Boolean isDelete = roomInfo.getIsDeleted() == 1;
        Boolean isCheckIn = roomInfo.getIsRelease() == ReleaseStatus.RELEASED;

        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        roomDetailVo.setApartmentItemVo(apartmentItemVo);
        roomDetailVo.setGraphInfoList(graphInfoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setFeeValueVoList(feeValueVoList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        roomDetailVo.setIsDelete(isDelete);
        roomDetailVo.setIsCheckIn(isCheckIn);

        /*
        if (!isDelete && isCheckIn) {
            return roomDetailVo;
        }
        */

        // 记录用户的浏览信息，具体方法使用异步线程，避免影响正常业务逻辑
        browsingHistoryService.saveHistory(LoginUserContext.get().getUserId(), id);

        return roomDetailVo;
    }

    @Override
    public IPage<RoomItemVo> pageItemByApartmentId(Page<Object> objectPage, Long id) {
        return roomInfoMapper.pageItemByApartmentId(objectPage, id);
    }
}




